package com.dacool.dialog;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.dacool.app.sale.R;
import com.dacool.fragment.AddressListFragment;
import com.dacool.http.BaseRequest;
import com.dacool.http.BaseRequest.ResponseHandler;
import com.dacool.http.GetAddressListRequest;
import com.dacool.model.DCAddress;
import com.dacool.util.ViewUtils;

public class ChooseAddressDialog extends BasePickerDialog {

	public ArrayList<DCAddress> getAddresses() {
		return addresses;
	}

	private ArrayList<DCAddress> addresses = new ArrayList<DCAddress>();

	public ChooseAddressDialog(Context context) {
		super(context);
		setAdapter(new ArrayAdapter<DCAddress>(getContext(), 0) {
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return addresses.size();
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				if (convertView == null) {
					View view = LayoutInflater.from(getContext()).inflate(
							R.layout.addresslistitem, null);
					ViewHolder holder = new ViewHolder();
					holder.tv1 = ViewUtils.findViewById(view,
							R.id.userInfo_name);
					holder.tv2 = ViewUtils.findViewById(view,
							R.id.userInfo_address);
					view.setTag(holder);
					convertView = view;
				}
				ViewHolder holder = (ViewHolder) convertView.getTag();
				DCAddress address = addresses.get(position);
				holder.tv1.setText(address.deliveryMan);
				holder.tv2.setText(address.address);
				return convertView;
			}

			class ViewHolder {
				TextView tv1;
				TextView tv2;

			}
		});
		new GetAddressListRequest().setHandler(new ResponseHandler() {

			@Override
			public void handleResponse(BaseRequest request, int statusCode,
					String errorMsg, String response) {
				if (statusCode != 200) {
					return;
				}
				boolean success = false;
				JSONObject json;
				try {
					json = new JSONObject(response);
					success = json.optBoolean("success", false);
					JSONArray array = json.getJSONArray("data");
					final ArrayList<DCAddress> addrs = new ArrayList<DCAddress>();
					for (int i = 0; i < array.length(); i++) {
						JSONObject obj = array.getJSONObject(i);
						DCAddress address = new DCAddress();
						address.id = obj.getInt("addrId");
						address.address = obj.getString("addr");
						address.deliveryMan = obj.getString("name");
						address.area = obj.getString("area");
						address.mobileNo = obj.getString("mobile");
						addrs.add(address);
					}
					if (success) {
						ViewUtils.runInMainThread(new Runnable() {

							@Override
							public void run() {
								addresses = addrs;
								getAdapter().notifyDataSetChanged();
							}
						});
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}).start();

		// TODO Auto-generated constructor stub
	}

}
